<?php
/**
 * @name : articlecontent.php
 * @date : 2015-01-17
 */
namespace haibao\cms\business;
use haibao\cms\model\data\ArticleContent as ArticleContentModel;
class ArticleContent extends \haibao\cms\business\Base{
    
    /**
     * 添加文章内容
     */
    public static function saveArticleContent($articleContent){
    	
    	if(!$articleContent){
    		
    		throw new \Exception('文章内容为空',self::DATA_EMPTY_NAME);
    	}
    	
    	$articleId = rand(1,1000);
    	$model = array();
    	foreach($articleContent as $k=>$content){
    		
    		$articleContentModel = new ArticleContentModel();
    		$articleContentModel->ArticleId = $articleId;
    		$articleContentModel->Content = $content;
    		$articleContentModel->CurrentPage = $k + 1;
    		$model[] = $articleContentModel;
    	}
    	
    	$articleContentData = new \haibao\cms\data\ArticleContent();
    	$articleContentData->addArticleContent($model);
    	
    	// TODO : 生成本地文件
    	//self::articleConvert($articleContent);
    }
    
    /**
     * 本地生成静态文章内容
     */
    public static function articleConvert($articleContent){
    	
    	$savePath = self::getSaveArticlePath();
    	self::createDir($savePath);
    	foreach ($articleContent as $key=>$content){
    		
    		file_put_contents($savePath.'a-'.$key.'.html', $content);
    	}
    }
    
    /**
     * 获取文章保存路径
     */
    private static function getSaveArticlePath(){
    	return \haibao\frame\Environment::path() . '/web/article/';
    	//return \haibao\frame\Environment::path() . $path;
    }
    

    /**
     * 创建文件保存路径
     */
    private static function createDir($path){
    	if(!is_dir($path)){
    		return is_writable($path['dirname']) ?: mkdir($path, 750, true);
    	}
    }
    
    /**
     * 根据filter获取文章内容
     */
    public static function getArticleContent($filter){
    	
    	$articleContentData = new \haibao\cms\data\ArticleContent();
    	return $articleContentData->getAll($filter);
    }
    
}